package com.example.administrator.yuejian2.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.activity.Club_DetailActivity;
import com.example.administrator.yuejian2.bean.ClubBean;
import com.example.administrator.yuejian2.viewholder.ClubViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.example.administrator.yuejian2.other.Url.IMAGEURL;

public class VLayoutClubAdapter extends DelegateAdapter.Adapter{

    private Context context;
    private LayoutInflater layoutInflater;
    private List<ClubBean> list;
    private LayoutHelper helper;
    private VirtualLayoutManager.LayoutParams layoutParams;

    public VLayoutClubAdapter(Context context, LayoutHelper helper)
    {
        this.layoutInflater = LayoutInflater.from(context);
        this.helper = helper;
        this.context=context;
    }

    public VLayoutClubAdapter(Context context, LayoutHelper helper, VirtualLayoutManager.LayoutParams layoutParams)
    {
        this.layoutInflater = LayoutInflater.from(context);
        this.helper = helper;
        this.context=context;
        this.layoutParams=layoutParams;
    }

    public void getClubData(List<ClubBean> list)
    {
        this.list=list;
        notifyDataSetChanged();
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return helper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.club_item,parent,false);
        return new VLayoutViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VLayoutViewHolder viewHolder=(VLayoutViewHolder)holder;
        final ClubBean bean=list.get(position);
        viewHolder.club_name.setText(bean.getClub_name());
        viewHolder.club_address.setText(bean.getClub_address());
        Picasso.with(context).load(IMAGEURL+bean.getClub_pic()).placeholder(R.mipmap.loading).into(viewHolder.club_pic);
        layoutParams=new VirtualLayoutManager.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,200);
        if (position % 2 == 0)
        {
            layoutParams.mAspectRatio=1.0f;
        }
        else {
            layoutParams.height=320+position % 5 * 20 ;
        }
        viewHolder.itemView.setLayoutParams(layoutParams);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, Club_DetailActivity.class);
                intent.putExtra("c_uid",bean.getClub_id());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list==null)
        {
            return 0;
        }
        else
        {
            return  list.size();
        }
    }


    class VLayoutViewHolder extends RecyclerView.ViewHolder{
        public ImageView club_pic;
        public TextView club_name;
        public TextView club_address;

        public VLayoutViewHolder(View itemView) {
            super(itemView);
            club_address=itemView.findViewById(R.id.club_address);
            club_pic=itemView.findViewById(R.id.club_pic);
            club_name=itemView.findViewById(R.id.club_name);
        }
    }
}
